package com.zk.yygh.dict.controller;

import com.zk.yygh.dict.service.DictService;
import com.zk.yygh.hosp.result.Result;
import com.zk.yygh.model.cmn.Dict;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author: 张凯
 * @date: 2022/5/21 - 21:58
 */
@Api("数据字典管理")
@RestController
@RequestMapping("/admin/dict")
//@CrossOrigin
public class DictController {
    @Autowired
    private DictService dictService;

    @ApiOperation("1.根据id获取其子一级列表数据")
    @GetMapping(value = "getChild/{id}")
    public Result getChildById(@PathVariable Long id){
        List<Dict> childList = dictService.getChildById(id);
        return Result.ok(childList);
    }

    @ApiOperation("2.数据字典导出")
    @GetMapping(value = "export/data")
    public void exportDict(HttpServletResponse response){
        dictService.exportDict(response);
    }

    @ApiOperation("3.数据字典导入")
    @GetMapping("import/data")
    public void importDict(MultipartFile file){
        System.out.println("导入操作");
        dictService.importDict(file);
    }

    /**
     *
     * @param dictCode 编码
     * @param value 值
     * @return 相关名字
     */
    @ApiOperation("4.1 查询医院等级或相关信息(编码，值)")
    @GetMapping("getName/{dictCode}/{value}")
    public String getNameByDictCodeAndValue(
            @PathVariable("dictCode") String dictCode,
            @PathVariable("value") Integer value){
        return dictService.getName(dictCode,value);
    }

    @ApiOperation("4.2 查询医院等级或相关信息（值）")
    @GetMapping("getName/{value}")
    public String getNameByDictCodeAndValue(
            @PathVariable("value") Integer value){
        return dictService.getName("",value);
    }

    @ApiOperation("5.1 根据dictType获取其子一级的name(所有省，省所属市，医院等级)")
    @GetMapping("getProCity/{dictType}")
    public Result getProCityName(@PathVariable("dictType") String dictType){
        return Result.ok(dictService.getProCityName(dictType));
    }

    @ApiOperation("5.2 省市二级联动：通过指定的省编码在service层根据其id获取指定的所有市")
    @GetMapping("getCityByDictType/{dictType}")
    public Result getCityByDictType(@PathVariable("dictType") String dictType){
        return Result.ok(dictService.getCityByProvince(dictType));
    }
}
